//
//  ESMainViewController.m
//  Eyeshot
//
//  Created by admin on 15/6/30.
//  Copyright (c) 2015年 eyeshot. All rights reserved.
//

#define kNavShowDuration 0.4
#define kCoverTag 200
#define kLeftMenuWidth (40 * kHorizen)
#define kLeftHeight (80 * kVertical)

#import "ESMainViewController.h"
#import "ESLeftMenu.h"
#import "ESNavigationController.h"
#import "ESGreatTopicVC.h"
#import "ESNewTopicVC.h"
#import "ESSubscribeViewController.h"
#import "ESSetViewController.h"
#import "ESRightViewController.h"
#import "OpenEyesViewController.h"
#import "ESCheckPost.h"

@interface ESMainViewController ()<ESLeftMenuDelegate,ESTopicVCDelegate>
@property (nonatomic, weak) ESNavigationController *showingNavigationController;
@property (nonatomic, weak) ESLeftMenu *leftMenu;
@property (nonatomic, strong) ESRightViewController *rightMenuVC;
@property (nonatomic,strong) ESNavigationController *nav;
@end

@implementation ESMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setBackgroupImage];
    
    [self setupAllChildVc];
   
    [self setupLeftMenu];
    
//    [self setupRightMenu];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hidden) name:@"dismiss" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reveal) name:@"reveal" object:nil];
}

- (void)setupLeftMenu {
    //添加左侧的menu
    ESLeftMenu *leftMenu = [[ESLeftMenu alloc] init];
    leftMenu.height = kLeftHeight;
    leftMenu.width = kLeftMenuWidth;
    leftMenu.y = 25 * kVertical;
    leftMenu.delegate = self;
    self.leftMenu = leftMenu;
    [self.view insertSubview:leftMenu atIndex:1]; //insert在背景图片上面
}

//- (void)setupRightMenu {
//    //添加右菜单
//    ESRightViewController *rightMenuVC = [[ESRightViewController alloc] init];
//    rightMenuVC.view.width = 60 * kHorizen + 20;
//    rightMenuVC.view.height = SCREEN_HEIGHT - STATUSBAR_HEIGHT;
//    rightMenuVC.view.x = self.view.width - rightMenuVC.view.width;
//    rightMenuVC.view.y = STATUSBAR_HEIGHT;
////    rightMenuVC.view.backgroundColor = [UIColor redColor];
//    [self.view insertSubview:rightMenuVC.view atIndex:1];
//    self.rightMenuVC = rightMenuVC;
//<<<<<<< HEAD
//    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:_rightMenuVC];
//    [self addChildViewController:nav];
//=======
//    ESNavigationController *nav = [[ESNavigationController alloc] initWithRootViewController:rightMenuVC];
//    nav.view.frame = rightMenuVC.view.frame;
//    nav.navigationBar.hidden = YES;
//    //    nav.view.backgroundColor = [UIColor yellowColor];
//    [self addChildViewController:nav];
//    [self.view insertSubview:nav.view atIndex:1];
//>>>>>>> e249774b9e955c8208be2286145b774c5019c613
//}

- (void)setupRightMenu {
    //添加右菜单
    ESRightViewController *rightMenuVC = [[ESRightViewController alloc] init];
    rightMenuVC.view.width = 60 * kHorizen + 20;
    rightMenuVC.view.height = SCREEN_HEIGHT - STATUSBAR_HEIGHT;
    rightMenuVC.view.x = self.view.width - rightMenuVC.view.width;
    rightMenuVC.view.y = STATUSBAR_HEIGHT;
    //    rightMenuVC.view.backgroundColor = [UIColor redColor];
    [self.view insertSubview:rightMenuVC.view atIndex:1];
    self.rightMenuVC = rightMenuVC;
}



/**
 * 先添加控制器,但不添加控制器的view,控制器的view由菜单栏的代理方法来管理
 */
- (void)setupAllChildVc {

    //添加“精华”控制器
    ESGreatTopicVC *great = [[ESGreatTopicVC alloc] init];
    great.delegate = self;
    [self setupVc:great title:@"精华"];

    
    //添加“新贴”控制器
    ESNewTopicVC *new = [[ESNewTopicVC alloc] init];
    new.delegate = self;
    [self setupVc:new title:@"新帖"];
    
    //添加“眼界”控制器
    OpenEyesViewController *eye = [[OpenEyesViewController alloc] init];
    [self setupVc:eye title:@"眼界"];
    
    //添加“订阅”控制器
    ESSubscribeViewController *subscribe = [[ESSubscribeViewController alloc] init];
    [self setupVc:subscribe title:@"订阅"];
    
    //添加“设置”控制器
    ESSetViewController *set = [[ESSetViewController alloc] init];
    [self setupVc:set title:@"设置"];
    
}

/**
 *  设置背景图片
 *
 */
- (void)setBackgroupImage {
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.frame = self.view.bounds;
    imageView.image = [UIImage imageNamed:@"sidebar_bg"];
    [self.view addSubview:imageView];
}



- (void)setupVc:(UIViewController *)vc title:(NSString *)title {
    
    ESNavigationController *nav = [[ESNavigationController alloc] initWithRootViewController:vc];
    [self addChildViewController:nav];
    
    //添加中间的标题titleView
    ESTitleView *titleView = [[ESTitleView alloc] init];
    titleView.width = 20 * kHorizen;
    titleView.height = 7 * kVertical;
    titleView.title = title;
    vc.navigationItem.titleView = titleView;
    
    vc.view.backgroundColor = [UIColor whiteColor];
    
    vc.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(clickLeftMenu) image:@"top_navigation_menuicon"];
//    vc.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(clickRightMenu) image:@"top_navigation_infoicon"];
    
    UIScreenEdgePanGestureRecognizer *screenLeftEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(clickLeftMenu)];
    screenLeftEdgePan.edges = UIRectEdgeLeft;
    [vc.view addGestureRecognizer:screenLeftEdgePan];
    
    UIScreenEdgePanGestureRecognizer *screenRightEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(clickRightMenu)];
    screenRightEdgePan.edges = UIRectEdgeRight;
    [vc.view addGestureRecognizer:screenRightEdgePan];
}

/**
 *  设置状态栏文字显示颜色
 *
 */
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

- (void)leftMenu:(ESLeftMenu *)menu didSelectedButtonFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    // 0.移除旧控制器的view
    ESNavigationController *oldNav = self.childViewControllers[fromIndex];
    [oldNav.view removeFromSuperview];
    
    // 1.显示新控制器的view
    ESNavigationController *newNav = self.childViewControllers[toIndex];
    [self.view addSubview:newNav.view];

    //2.设置新控制的transform跟旧控制器一样,确保第一次运行的时候newNav的transform不为CGAffineTransformIdentity
    // 一个导航控制器的view第一次显示到它的父控件上时，如果transform的缩放值被改了，上面的20高度当时是不会出来，所以要再添加了view之后在设置view的transform
    newNav.view.transform = oldNav.view.transform;
    
    // 设置阴影
    newNav.view.layer.shadowColor = [UIColor darkGrayColor].CGColor;
    newNav.view.layer.shadowOffset = CGSizeMake(-3, 0);
    newNav.view.layer.shadowOpacity = 0.2;
    
    //3.设置当前正在显示的控制器
    self.showingNavigationController = newNav;
    
    //4.设置transform为CGAffineTransformIdentity及移除遮盖,等价于点击遮盖
    [self coverClick:[self.showingNavigationController.view viewWithTag:kCoverTag]];

}


- (void)clickLeftMenu {
    self.leftMenu.hidden = NO;
    self.rightMenuVC.view.hidden = YES;
    [UIView animateWithDuration:kNavShowDuration animations:^{
        
        if (CGAffineTransformIsIdentity(self.showingNavigationController.view.transform)) {
            // 缩放比例
            //12 * kVertical 距离顶部、底部的高度
            CGFloat navH = [UIScreen mainScreen].bounds.size.height - 2 * 12 * kVertical;
            CGFloat scale = navH / [UIScreen mainScreen].bounds.size.height;
            
            // 菜单左边的间距
            CGFloat leftMenuMargin = [UIScreen mainScreen].bounds.size.width * (1 - scale) * 0.5;
            // 50 * kHorizen 左边菜单的宽度
            CGFloat translateX = kLeftMenuWidth - leftMenuMargin;
            
            CGFloat topMargin = [UIScreen mainScreen].bounds.size.height * (1 - scale) * 0.5;
            CGFloat translateY = topMargin - 12 * kVertical;
            
            // 缩放
            CGAffineTransform scaleForm = CGAffineTransformMakeScale(scale, scale);
            // 平移
            CGAffineTransform translateForm = CGAffineTransformTranslate(scaleForm, translateX / scale, -translateY / scale);
            
            self.showingNavigationController.view.transform = translateForm;
            
            // 添加一个遮盖
            UIControl *cover = [[UIControl alloc] init];
            cover.frame = self.showingNavigationController.view.bounds;
            [self.showingNavigationController.view addSubview:cover];
            cover.tag = kCoverTag;
            [cover addTarget:self action:@selector(coverClick:) forControlEvents:UIControlEventAllTouchEvents];
        }
        
    }];
}

- (void)clickRightMenu {    
    self.leftMenu.hidden = YES;
    self.rightMenuVC.view.hidden = NO;
    
    [UIView animateWithDuration:kNavShowDuration animations:^{
        
        if (CGAffineTransformIsIdentity(self.showingNavigationController.view.transform)) {
            // 缩放比例
            //12 * kVertical 距离顶部、底部的高度
            CGFloat navH = [UIScreen mainScreen].bounds.size.height - 2 * 12 * kVertical;
            CGFloat scale = navH / [UIScreen mainScreen].bounds.size.height;
            
            // 菜单左边的间距
            CGFloat leftMenuMargin = [UIScreen mainScreen].bounds.size.width * (1 - scale) * 0.5;
            // 50 * kHorizen 左边菜单的宽度
            CGFloat translateX = leftMenuMargin - self.rightMenuVC.view.width;
            
            CGFloat topMargin = [UIScreen mainScreen].bounds.size.height * (1 - scale) * 0.5;
            CGFloat translateY = 12 * kVertical - topMargin;
            
            // 缩放
            CGAffineTransform scaleForm = CGAffineTransformMakeScale(scale, scale);
            // 平移
            CGAffineTransform translateForm = CGAffineTransformTranslate(scaleForm, translateX / scale, -translateY / scale);
            
            self.showingNavigationController.view.transform = translateForm;
            
            // 添加一个遮盖
            UIControl *cover = [[UIControl alloc] init];
            cover.frame = self.showingNavigationController.view.bounds;
            [self.showingNavigationController.view addSubview:cover];
            cover.backgroundColor = [UIColor clearColor];
            cover.tag = kCoverTag;
            [cover addTarget:self action:@selector(coverClick:) forControlEvents:UIControlEventAllTouchEvents];
        }
        
    }completion:^(BOOL finished) {
        [self.rightMenuVC didShow];
    }];
}

- (void)coverClick:(UIView *)cover
{
    [UIView animateWithDuration:kNavShowDuration animations:^{
        self.showingNavigationController.view.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        [cover removeFromSuperview];
    }];
}

- (void)hidden
{
    self.showingNavigationController.view.hidden = YES;
}
- (void)reveal
{
    self.showingNavigationController.view.hidden = NO;
}
- (void)didExceedLeftBoundaryOfTopicVC:(ESTopicVC *)topicVc {
    [self clickLeftMenu];
}

- (void)didExceedRightBoundaryOfTopicVC:(ESTopicVC *)topicVc {
    [self clickRightMenu];
}

@end
